﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OUSMS.AppLogic.DataAccessLayer;
using OUSMS.AppLogic.BusinessLogicLayer;
using System.Web.ModelBinding;
using AjaxControlToolkit;
namespace OUSMS
{
    public partial class AssignMotion : System.Web.UI.Page
    {
        public string departments;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MotionsBLL motionBLL = new MotionsBLL();
                Motions motionEntity = motionBLL.GetMotionsByID(Convert.ToInt64(Request.QueryString["MID"]));

                foreach (MotionDepartments department in motionEntity.MotionDepartments)
                {
                    departments += department.Departments.Name + "; ";
                }

                UsersBLL userBLL = new UsersBLL();
                TabContainer tabContainer = motionDetailFormView.FindControl("TabContainer") as TabContainer;

                CheckBoxList schooleLeaderChkboxList = tabContainer.Tabs[0].FindControl("schooleLeaderChkboxList") as CheckBoxList;
                schooleLeaderChkboxList.DataSource = userBLL.GetAssignPersonByRole("校领导");
                schooleLeaderChkboxList.DataBind();

                CheckBoxList departmentLeaderCheckBoxList = tabContainer.Tabs[1].FindControl("departmentLeaderCheckBoxList") as CheckBoxList;
                departmentLeaderCheckBoxList.DataSource = userBLL.GetAssignPersonByRole("部门负责人");
                departmentLeaderCheckBoxList.DataBind();

                CheckBoxList expertCheckBoxList = tabContainer.Tabs[2].FindControl("expertCheckBoxList") as CheckBoxList;
                expertCheckBoxList.DataSource = userBLL.GetAssignPersonByRole("评审专家");
                expertCheckBoxList.DataBind();
            }
        }

        /// <summary>
        /// 选择了专家，将当前提案转发给制定专家，当前状态跳转为“等待专家反馈审核意见”；
        /// 如果没有选择任何专家，当前提案直接跳转到“等待转发部门领导审核”
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void okBtn_Click(object sender, EventArgs e)
        {
            bool isAssign = false;
            MotionReviewerCommentBLL motionReviewerCommentBLL = new MotionReviewerCommentBLL();
            
            MotionsBLL motionsBLL = new MotionsBLL();
            Motions motions = motionsBLL.GetMotionsByID(Convert.ToInt64(Request.QueryString["MID"]));

            TabContainer tabContainer = motionDetailFormView.FindControl("TabContainer") as TabContainer;
            MotionReviewerComments comment;
            CheckBoxList schooleLeaderChkboxList = tabContainer.Tabs[0].FindControl("schooleLeaderChkboxList") as CheckBoxList;          
            for (int index = 0; index < schooleLeaderChkboxList.Items.Count; index++)
            {
                if (schooleLeaderChkboxList.Items[index].Selected)
                {
                    isAssign = true;
                    comment = new MotionReviewerComments();
                    comment.DistributeDateTime = DateTime.Now;
                    comment.MotionID = Convert.ToInt64(Request.QueryString["MID"]);
                    comment.ReviewerID = Convert.ToInt64(schooleLeaderChkboxList.Items[index].Value);
                    comment.RoleType = "校领导";
                    motionReviewerCommentBLL.CreateMotionReviewerComment(comment);
                }
            }
            CheckBoxList departmentLeaderCheckBoxList = tabContainer.Tabs[1].FindControl("departmentLeaderCheckBoxList") as CheckBoxList;
            for (int index = 0; index < departmentLeaderCheckBoxList.Items.Count; index++)
            {
                if (departmentLeaderCheckBoxList.Items[index].Selected)
                {
                    isAssign = true;
                    comment = new MotionReviewerComments();
                    comment.DistributeDateTime = DateTime.Now;
                    comment.MotionID = Convert.ToInt64(Request.QueryString["MID"]);
                    comment.ReviewerID = Convert.ToInt64(schooleLeaderChkboxList.Items[index].Value);
                    comment.RoleType = "部门负责人";
                    motionReviewerCommentBLL.CreateMotionReviewerComment(comment);
                }
            }
            CheckBoxList expertCheckBoxList = tabContainer.Tabs[2].FindControl("expertCheckBoxList") as CheckBoxList;
            for (int index = 0; index < expertCheckBoxList.Items.Count; index++)
            {
                if (expertCheckBoxList.Items[index].Selected)
                {
                    isAssign = true;
                    comment = new MotionReviewerComments();
                    comment.DistributeDateTime = DateTime.Now;
                    comment.MotionID = Convert.ToInt64(Request.QueryString["MID"]);
                    comment.ReviewerID = Convert.ToInt64(schooleLeaderChkboxList.Items[index].Value);
                    comment.RoleType = "评审专家";
                    motionReviewerCommentBLL.CreateMotionReviewerComment(comment);
                }
            }
            motions.TransferDateTime = DateTime.Now;
            motions.Status = "待立项";
            if (isAssign)
            {
                TextBox deadlineDateTextBox = motionDetailFormView.FindControl("deadlineDateTextBox") as TextBox;
                if (!string.IsNullOrEmpty(deadlineDateTextBox.Text.Trim()))
                {
                    motions.FeedbackDeadlineDateTime = Convert.ToDateTime(deadlineDateTextBox.Text.Trim());
                }
                else
                {
                    motions.FeedbackDeadlineDateTime = DateTime.Now.AddDays(7);
                }
            }
            motionsBLL.Save();
            Response.Redirect("WaitForTransferMotionList.aspx");
        }

        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("WaitForTransferMotionList.aspx");
        }

        // id 参数应与控件上设置的 DataKeyNames 值匹配
        // 或用值提供程序特性装饰，例如 [QueryString]int id
        public Motions motionDetailFormView_GetItem([QueryString("MID")]long id)
        {
            MotionsBLL motionsBLL = new MotionsBLL();
            return motionsBLL.GetMotionsByID(id);
        }        
    }
}